package cn.stronglink.asset.message.mq.consumer;

import java.util.List;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.stronglink.asset.message.mq.consumer.entity.BaseMessage;
import cn.stronglink.asset.message.mq.consumer.entity.Reader003R;
import cn.stronglink.asset.message.mq.consumer.entity.Reader003Udev;
import cn.stronglink.asset.message.mq.consumer.entity.Reader008DevAddr;
import cn.stronglink.asset.module.rfidState.service.RfidStateCPService;

@Component("ruReceiver")
public class RuReceiver implements MessageListener {

	@Autowired
	private RfidStateCPService rfidStateCPService;
	
	@Override
	public void onMessage(Message message) {
		try {
			if (!(message instanceof ActiveMQTextMessage)) {
				return;
			}
			TextMessage tm = (TextMessage) message;
			String msg = tm.getText();
			BaseMessage bm = JSON.parseObject(msg, BaseMessage.class);
			String rfidType = bm.getRfidtype();
			String actionCode = bm.getActioncode();
			if("RU1000".equals(rfidType)){
				if("reader003".equals(actionCode)){
					System.out.println("**********接收到RU1000采集到的标签："+bm.getAwsPostdata().toString());
					Reader003R reader003 = JSONObject.parseObject(bm.getAwsPostdata().toString(), Reader003R.class);
					String cabinetCode = reader003.getDevAddrList().get(0).getDevAddrCode();// 机柜编号
					List<Reader003Udev> rfidAndUList = reader003.getDevAddrList().get(0).getUdevInfo();//采集过来的rfid和u位信息
					//根据采集信息比对台账并报警(产品里的项目)
					rfidStateCPService.checkU(cabinetCode,rfidAndUList);
					
				}else if("reader008".equals(actionCode)){
					System.out.println("********检测到机柜有开关门状态变化********");
					Reader008DevAddr postData = JSONObject.parseObject(bm.getAwsPostdata().toString(), Reader008DevAddr.class);
					String doorState = postData.getCdoorstate();//状态 0关  1开
					String devAddrCode = postData.getDevAddrCode();//机柜转换器编码
					
				}else if("reader007".equals(actionCode)){					
//					System.out.println("********插拔天线数据********");
//					Reader007DevAddr reader007 = JSONObject.parseObject(bm.getAwsPostdata().toString(), Reader007DevAddr.class);
//					try {
//						apiService.sendAntennaData(reader007);
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
				}
			}else if("smarrack".equals(rfidType)) {
				if("reader003".equals(actionCode)){
					System.out.println("**********接收到smarrack采集到的标签："+bm.getAwsPostdata().toString());
					Reader003R reader003 = JSONObject.parseObject(bm.getAwsPostdata().toString(), Reader003R.class);
					String cabinetCode = reader003.getDevAddrList().get(0).getDevAddrCode();// 机柜编号
					List<Reader003Udev> rfidAndUList = reader003.getDevAddrList().get(0).getUdevInfo();//采集过来的rfid和u位信息
					//根据采集信息比对台账并报警(产品里的项目)
					rfidStateCPService.checkU(cabinetCode,rfidAndUList); //英诺
					
				}
			}
			
		} catch (JMSException e) {
			e.printStackTrace();
		}

	}
}
